﻿using GRPCClass;
using SdtpsBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SdtpsBase.Util;

namespace SdtpsNodeLibrary
{
    public class TaskWorker : ITaskWorker
    {
        System.Threading.Thread mythd;
        public void Start()
        {
            mythd = new System.Threading.Thread(new System.Threading.ThreadStart(Execute));
            mythd.Start();

        }
        public bool isStop
        {
            get;
            set;
        }
        public event ExecuteEventHandler ExecuteEvent;
        public void Execute()
        {
            do
            {
                if (TaskNodeHandle.TaskRequestList.Count > 0)
                {
                    //获取到一个待执行的任务
                    TaskRequest mytr = TaskNodeHandle.TaskRequestList.Dequeue();
                    //放到执行列表中
                    TaskNodeHandle.taskReplyRuning.Add(mytr);
                    if (this.ExecuteEvent == null)
                    {
                        //执行任务处理
                        TaskRun.ExecutePlug(mytr);
                    }
                    else
                    {
                        ExecuteEvent(this, mytr);
                    }
                }
                System.Threading.Thread.Sleep(1000);
            } while (!isStop);
        }
        /// <summary>
        /// 执行完成后回复中心节点
        /// </summary>
        /// <param name="trvalue"></param>
        /// <param name="tpvalue"></param>
        public void Task_end(TaskRequest trvalue, TaskReply tpvalue)
        {
            TaskRun.Task_End(trvalue, tpvalue);
        }

    }
}
